package cn.net.mugui.net.pc.panel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.GroupLayout;
import javax.swing.GroupLayout.Alignment;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle.ComponentPlacement;

import com.mugui.Dui.DButton;
import com.mugui.base.base.Autowired;
import com.mugui.base.base.Component;

import cn.net.mugui.net.pc.manager.FunctionUI;
import cn.net.mugui.net.pc.task.PushTask;
import cn.net.mugui.net.pc.task.WebCamTask;

@Component
public class WebCamUI extends FunctionUI {

	public WebCamUI() {
		setTitle("家庭摄像头");
		setMenu_name("家庭摄像头");
		setLayout(new BorderLayout(0, 0));

		JPanel panel_1 = new JPanel();
		add(panel_1, BorderLayout.NORTH);
		setBackground(null);
		DButton button = new DButton("开始推流", (Color) null);
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getActionCommand().equals("开始推流")) {
					button.setText("停止");
					ur = textField.getText();
					pushTask.startZhibo(ur);
				} else {
					button.setText("开始推流");
					pushTask.stopZhibo();
				}

			}
		});

		DButton button_1 = new DButton("开启摄像", (Color) null);
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (e.getActionCommand().equals("开启摄像")) {
					button_1.setText("停止摄像");
					pushTask.start("");
					panel.start();
				} else {
					button_1.setText("开启摄像");
					pushTask.stop("");
					panel.stop();
				}
			}
		});

		textField = new JTextField();
		textField.setText(ur);
		textField.setColumns(128);
		GroupLayout gl_panel_1 = new GroupLayout(panel_1);
		gl_panel_1.setHorizontalGroup(gl_panel_1.createParallelGroup(Alignment.LEADING).addGroup(gl_panel_1
				.createSequentialGroup().addContainerGap()
				.addComponent(button_1, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE,
						GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(ComponentPlacement.RELATED)
				.addComponent(button, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)
				.addPreferredGap(ComponentPlacement.RELATED)
				.addComponent(textField, GroupLayout.DEFAULT_SIZE, 0, Short.MAX_VALUE).addContainerGap()));
		gl_panel_1.setVerticalGroup(gl_panel_1.createParallelGroup(Alignment.LEADING).addGroup(gl_panel_1
				.createSequentialGroup().addGap(5)
				.addGroup(gl_panel_1.createParallelGroup(Alignment.BASELINE)
						.addComponent(button_1, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(button, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
						.addComponent(textField))
				.addGap(5)));
		panel_1.setLayout(gl_panel_1);

		panel_2 = new JPanel() {
			@Override
			public void paint(Graphics graphics) {
//				super.paint(graphics);
				if (convert != null) {
					graphics.setColor(Color.black);
					graphics.fill3DRect(0, 0, panel_2.getWidth(), panel_2.getHeight(), true);

					int width = convert.getWidth();
					int height = convert.getHeight();

					if (width < panel_2.getWidth()) {
						double d = (width) / (double) panel_2.getWidth();
						width = panel_2.getWidth();
						height = (int) (height / d);
					} else {
						double d = (width) / (double) panel_2.getWidth();
						width = panel_2.getWidth();
						height = (int) (height / d);
					}
					if (height > panel_2.getHeight()) {
						double d = (height) / (double) panel_2.getHeight();
						width = (int) (width / d);
						height = panel_2.getHeight();
					} else {
						double d = (width) / (double) panel_2.getWidth();
						width = (int) (width / d);
					}

//					graphics.drawImage(convert, 0, 0, null);
					graphics.drawImage(convert, panel_2.getWidth() / 2 - width / 2,
							panel_2.getHeight() / 2 - height / 2, panel_2.getWidth() / 2 - width / 2 + width,
							panel_2.getHeight() / 2 - height / 2 + height, convert.getWidth(), 0, 0,
							convert.getHeight(), null);
				}
			}
		};
		add(panel_2, BorderLayout.CENTER);
	}

	@Autowired
	WebCamTask panel = null;
	String ur = "rtmp://live-push.bilivideo.com/live-bvc/?streamname=live_6548442_2322856&key=238aa52a65543465578005a85f29b56f&schedule=rtmp&pflag=9";

	@Override
	public void init() {
	}

	@Autowired
	PushTask pushTask;

	@Override
	public void quit() {
	}

	@Override
	public void dataInit() {

	}

	@Override
	public void dataSave() {

	}

	private JTextField textField;
	private JPanel panel_2;

	BufferedImage convert = null;

	public void show(BufferedImage convert) {
		this.convert = convert;
		panel_2.repaint();
	}
}
